home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / tool chest / development kits / mpw related / mpw script tips 1.1.1 / sample scripts / protogen < prev    next >
Encoding:
Text File  |  1991-08-16  |  8.7 KB  |  288 lines

  1. #    ProtoGen
  2. #    MPW Shell Script
  3. #    Original ProtoGen by C.K. Haun
  4. #    First revision by David D. Ely 
  5. #    Second revision by Gina Cherry • August 16, 1991
  6. #    Copyright:    © 1991 by Apple Computer, Inc., all rights reserved.
  7. #
  8. #    Usage: 
  9. #        ProtoGen [<input file>…] [-e] [-n] [-o <output file>]
  10. #
  11. #    Function: 
  12. #        Prototype generator for ANSI C source code.
  13. #        Puts prototypes from the C source files specified on the command line into a prototype file.  
  14. #        If no input file is specified, ProtoGen operates on the target file.  If no output file is specified, 
  15. #        the output defaults to <input file>.proto, using the first input file specified on the command line
  16. #        for the output file name.  
  17. #        If -e is specified, all function declarations in the output file will be proceeded by extern.
  18. #        If -o <output file> is specified, output will be appended to the named file.  If -n is specified, 
  19. #        ProtoGen will create prototypes for old-style C source code.
  20. #
  21. #    Note:
  22. #        ProtoGen can be used with Commando.
  23.  
  24. #    Don't exit on error.
  25.     Set Exit 0
  26.  
  27. # Want searches to be case sensitive.
  28.     Set CaseSensitive 1
  29.  
  30. # Initialize the file variables.
  31.     Set Files ""
  32.     Set protoFile ""
  33.     Set firstFile ""
  34.  
  35. # Initially don't declare functions as externs.
  36.     Set Extern 0
  37.  
  38. # Initially set to create prototypes for ANSI C
  39.     Set Ansi 1
  40.  
  41. #    Initialize Word variable.
  42.     Set Word '[a-zA-Z_0-9]+'
  43.  
  44. # Loop through parameters.
  45.     Loop
  46.     # Break if no more parameters.
  47.         Break If "{1}" == ""
  48.         
  49.         If "{1}" == '-o'
  50.         #    Set protoFile to the specified output file if -o has not already been given as an option.
  51.             If "{protoFile}" == ""
  52.             # Get next parameter
  53.                 Shift 1
  54.             # If there is no next parameter, print error message and exit program.
  55.                 If "{1}" == ""
  56.                     Echo "### {0}: Option ∂"{1}∂" must be followed by a file name."
  57.                     Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]" 
  58.                     Exit 1    
  59.             # Otherwise, set the output file to the next parameter.
  60.                 Else
  61.                     Set protoFile "{1}"
  62.                 End
  63.         #    Otherwise, write an error message and exit script.
  64.             Else
  65.                 Echo "### "{0}": Option ∂"{1}∂" multiply defined."
  66.                 Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]" 
  67.                 Exit 1
  68.             End 
  69.         
  70.     # If -e option is specified, want to create prototypes as externs.    
  71.         Else If "{1}" == '-e'
  72.         #    Make sure Extern hasn't already been set.
  73.             If {Extern} == 0
  74.                 Set Extern 1
  75.         #    If it has, write an error message and exit script.
  76.             Else
  77.                 Echo "### {0}: Option ∂"{1}∂" multiply defined."
  78.                 Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]" 
  79.                 Exit 1
  80.             End 
  81.             
  82.     # If -n option is specified, want to prototype old-style C programs.
  83.         Else If "{1}" == '-n'
  84.         #    Make sure Ansi hasn't already been set to 0.
  85.             If {Ansi} == 1
  86.                 Set Ansi 0
  87.         #    If it has, write an error message and exit script.
  88.             Else
  89.                 Echo "### {0}: Option ∂"{1}∂" multiply defined."
  90.                 Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]" 
  91.                 Exit 1
  92.             End 
  93.         
  94.     # If bad option is given, write error message and exit script.    
  95.         Else If "{1}" =~ /∂-≈/ || "{1}" == '-'
  96.             Echo "### {0}: ∂"{1}∂" is not an option."
  97.             Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]"  
  98.             Exit 1
  99.             
  100.     # If parameter is not an option, add it to the list of input files.    
  101.         Else 
  102.             Set Files "{Files} '{1}'"
  103.         #    If this is the first file, set the firstFile variable
  104.             If firstFile == ""
  105.                 Set firstFile "{1}"
  106.             End
  107.         End >> Dev:StdErr
  108.         
  109.     # Get next parameter.
  110.         Shift 1
  111.     End
  112.  
  113. # If no input file was specified, set input file to the target file.
  114.     If "{Files}" == ""
  115.         Set Files "'{Target}'"
  116.     End
  117.  
  118. # If no output file was specified, set output file to <input file>.proto.
  119.     If "{protoFile}" == ""
  120.         If {firstFile} == ""
  121.             Set protoFile "{Target}".proto
  122.         Else
  123.             Set protoFile "{firstFile}".proto
  124.         End
  125.     End
  126.  
  127. # Create the output window by redirecting the output from the Echo command. If protoFile does not 
  128. #    exist, it will be created; otherwise, the output will be appended to the existing file.
  129.     Echo "∂n∂/*----- ProtoGen prototypes - `date` -----*∂/∂n" >>"{protoFile}"
  130.  
  131. # Make the output file the active window.
  132.     Open "{protoFile}"
  133.         
  134. # Mark end of output file.
  135.     Find ∞ "{protoFile}"
  136.     Mark -y § START "{protoFile}"
  137.         
  138. For fName in {Files}
  139.     # Record whether fName is already open.
  140.         Set fullName "`Files -i -f "{fName}" ≥ Dev:Null`"
  141.         Set wasOpen `Evaluate "∂`Windows∂`" =~ /≈{fullName}≈/`
  142.         
  143.     # Open the input file as the target window. Discard diagnostic output from the Open command,
  144.     #    since it's not needed.
  145.         Open -t "{fName}" ≥ Dev:Null
  146.     
  147.     # If input file could not be opened, print error message and continue with next iteration of loop.
  148.         If {Status} != 0
  149.             Echo "### {0}: File {fName} not found." >> Dev:StdErr
  150.             Continue
  151.         End
  152.     
  153.     #    Echo the name of the input file to the output file.
  154.         Echo "∂n∂/*----- {fName} -----*∂/∂n" >>"{protoFile}"
  155.     
  156.     # Go to the top of the input file.
  157.         Find • "{fName}"
  158.     
  159.     # Loop until there are no more function declarations in the input file.
  160.         Loop
  161.         
  162.             # Position cursor at beginning of function.
  163.                 Find Δ/•{Word}[ ∂t∂n∂*a-zA-Z_0-9]*∂([ ∂t∂n∂*a-zA-Z_0-9∂[∂],]*[∂)][¬;∂)∂n]*∞/ "{fName}"
  164.             
  165.             # Break if no more functions.
  166.                 Break If {Status}
  167.             
  168.             # Select the entire line.
  169.                 Find !0 "{fName}"
  170.             
  171.             # If externs, write extern to input file.
  172.                 If {Extern}
  173.                     Echo -n "extern " >> "{protoFile}"
  174.                 End
  175.             
  176.             # Write function header to output file.
  177.                 Catenate "{fName}.§" >> "{protoFile}"
  178.         
  179.             
  180.             # If prototyping old-style C file:
  181.                 If {Ansi} == 0
  182.             
  183.                 # Position cursor after left parenthesis of current function in output file.  
  184.                     Find \∂(\Δ "{protoFile}"
  185.                 
  186.                 # Loop through parameter list in the currently selected function in the output file.
  187.                     Loop
  188.                     
  189.                         # Select the next formal parameter name in the list.
  190.                             Find /[A-Za-z_0-9∂)]+/ "{protoFile}"
  191.                         
  192.                         # Break if have reached the right parenthesis.
  193.                             Break If " `Catenate "{protoFile}.§"`" =~ / *∂)/
  194.                         
  195.                         # If the parameter is a word ending in a right parenthesis, separate the word from 
  196.                         #    the parenthesis.
  197.                             If "`Catenate "{protoFile}.§"`" =~ /≈∂)/
  198.                                 Find §¡0 "{protoFile}"
  199.                                 Find /{Word}/ "{protoFile}"
  200.                             End
  201.                         
  202.                         # Use Evaluate statement to make the selected text in the output file (the name of the 
  203.                         #    formal parameter) available to the shell using the ®1 variable.
  204.                             (Evaluate "`Catenate "{protoFile}.§"`" =~ /({Word})®1/) ∑ Dev:Null
  205.                         
  206.                         # Mark the position of the current parameter in the output file.
  207.                             Mark § -y {0}.Param "{protoFile}"
  208.                         
  209.                         # Find the parameter name (including any '*'s associated with the parameter) in the
  210.                         #    input file.
  211.                             Find /[∂*]*{®1}/ "{fName}"
  212.                         
  213.                         # Copy the full parameter name to the clipboard.
  214.                             Copy § "{fName}"
  215.                         
  216.                         # Write the full parameter name to the output file.
  217.                             Paste {0}.Param "{protoFile}"
  218.                         
  219.                         # Go to the beginning of the current line in the input file.
  220.                             Find \•\ "{fName}"
  221.                         
  222.                         # Find the first word on the line, which is the type of the current parameter.
  223.                             Find /{Word}[ ∂t]/ "{fName}"
  224.                         
  225.                         # Copy the parameter type to the clipboard.
  226.                             Copy § "{fName}"
  227.                         
  228.                         # Write the parameter type to the output file before the parameter name.
  229.                             Paste Δ{0}.Param "{protoFile}"
  230.                         
  231.                         # Position cursor at the end of the parameter name in the output file.
  232.                             Find {0}.ParamΔ "{protoFile}"
  233.                     End
  234.             End
  235.     
  236.     
  237.         # Insert a blank line after function header in the output file.
  238.             Echo >> "{protoFile}"
  239.         
  240.         # Skip to end of function in the input file.
  241.             Find /∂{/ "{fName}"
  242.             MatchIt -c "{fName}"
  243.     
  244.     End
  245.     
  246.     # Close input file if file was not open before; don't save changes.
  247.         If !"{wasOpen}"
  248.             Close -n "{fName}"
  249.         End
  250.     End
  251.  
  252. # Add a semicolon at the end of each line in the output file. 
  253.     Find ΔSTART "{protoFile}"
  254.     Replace -c ∞ /∂)/ ");" "{protoFile}"
  255.  
  256. # Insert void in function headers with an empty parameter list.    
  257.     Find ΔSTART "{protoFile}"
  258.     Replace -c ∞ /∂([ ∂t]*∂)/ '(void)' "{protoFile}"
  259.  
  260. # Insert tab between the function name and the left paren.
  261.     Find ΔSTART "{protoFile}"
  262.     Replace -c ∞ /({Word})®1[ ∂t]*∂(/ "®1∂t∂(" "{protoFile}"
  263.  
  264. # Do some special formatting for old-style C prototypes.
  265.     If {Ansi} == 0
  266.     
  267.         # Insert comma and space between all parameters.
  268.             Find ΔSTART "{protoFile}"
  269.             Replace -c ∞ /[,]({Word})®1/ ", ®1" "{protoFile}"
  270.         
  271.         # Remove extra tabs and spaces.
  272.             Find ΔSTART "{protoFile}"
  273.             Replace -c ∞ /({Word})®1[ ∂t]+([A-Za-z_0-9∂*]+)®2/ "®1 ®2" "{protoFile}"
  274.         
  275.         # Remove marker from the output file.
  276.             Unmark {0}.Param "{protoFile}" 
  277.     End
  278.  
  279. # Insert blank lines at the end of the output file.
  280.     Echo "∂n∂n" >> "{protoFile}"
  281.  
  282. # Remove the marker from the output file.
  283.     Unmark START "{protoFile}" 
  284.  
  285. # Close output file; save changes.
  286.     Close -y "{protoFile}"
  287.  
  288.